package com.hfjyz.judge.judge.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.hfjyz.base.action.GridBaseAction;
import com.hfjyz.base.constant.BaseConstant;
import com.hfjyz.base.exception.BaseException;
import com.hfjyz.judge.judge.bean.JudgeBean;
import com.hfjyz.judge.judge.bean.JudgeExcelBean;
import com.hfjyz.judge.judge.bean.JudgeSearchBean;
import com.hfjyz.judge.judge.service.JudgeService;
import com.hfjyz.system.category.bean.CategoryBean;
import com.hfjyz.system.category.service.CategoryService;
import com.hfjyz.system.user.bean.UserBean;

public class JudgeAction extends GridBaseAction {
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 1748589881693831514L;
    private static final Log SYS_LOG = LogFactory.getLog(BaseConstant.SYS_LOG);
    private JudgeService service = null;
    private CategoryService categoryService = null;
    private JudgeBean judge = null;
    private String selectIds = null;
    private String judgeId = null;
    private JudgeExcelBean judgeDetail = null;

    protected JudgeSearchBean judgeSearch = null;

    public JudgeService getService() {
        return service;
    }

    public void setService(JudgeService service) {
        this.service = service;
    }

    public CategoryService getCategoryService() {
        return categoryService;
    }

    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    public JudgeBean getJudge() {
        return judge;
    }

    public void setJudge(JudgeBean judge) {
        this.judge = judge;
    }

    public JudgeExcelBean getJudgeDetail() {
        return judgeDetail;
    }

    public void setJudgeDetail(JudgeExcelBean judgeDetail) {
        this.judgeDetail = judgeDetail;
    }

    public JudgeSearchBean getJudgeSearch() {
        return judgeSearch;
    }

    public void setJudgeSearch(JudgeSearchBean judgeSearch) {
        this.judgeSearch = judgeSearch;
    }

    public String getSelectIds() {
        return selectIds;
    }

    public void setSelectIds(String selectIds) {
        this.selectIds = selectIds;
    }

    public String getJudgeId() {
        return judgeId;
    }

    public void setJudgeId(String judgeId) {
        this.judgeId = judgeId;
    }

    @Override
    protected List<JudgeBean> getBeanList() throws BaseException {
        HttpSession session = ServletActionContext.getRequest().getSession();
        session.setAttribute(BaseConstant.SESSION_CONDITION_KEY, judgeSearch);

        return service.findJudge(getSearchCondition(), judgeSearch);
    }

    @Override
    protected long getTotalCount() throws BaseException {
        return service.getJudgeCount(judgeSearch);
    }

    public String save() throws BaseException {
        map = new HashMap<String, Object>();
        try {
            HttpSession session = ServletActionContext.getRequest().getSession();
            UserBean user = (UserBean) session.getAttribute(BaseConstant.SESSION_USER_INFO_KEY);

            service.saveJudge(judge, user.getId());
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        } catch (Exception e) {
            SYS_LOG.error("发生未知错误！", e);
            map.put("errorMsg", "发生未知错误，请联系管理员！");
            return ERROR;
        }
    }

    public String updateInit() {
        map = new HashMap<String, Object>();
        try {
            Integer id = Integer.valueOf(selectIds);
            judge = service.updateInitJudge(id);
            List<CategoryBean> categoryList = categoryService.findCategory(3);
            map.put("judge", judge);
            map.put("categorys", categoryList);
            
            CategoryBean bean = null;
            for (CategoryBean category : categoryList) {
                if (category.getId().intValue() == judge.getCategoryId().intValue()) {
                    bean = category;
                    break;
                }
            }
            
            if (bean == null) {
                bean = categoryService.updateInitCategory(judge.getCategoryId());
                map.put("level1", bean.getParent());
                map.put("level2", bean.getId());
            } else {
                map.put("level1", judge.getCategoryId());
                map.put("level2", "");
            }
            
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        }
    }

    public String delete() {
        map = new HashMap<String, Object>();
        try {
            String[] idArray = selectIds.split(",");
            List<Integer> idList = new ArrayList<Integer>();
            for (String id : idArray) {
                idList.add(Integer.valueOf(id));
            }
            service.deleteJudge(idList);
        return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        } catch (Exception e) {
            SYS_LOG.error("发生未知错误！", e);
            map.put("errorMsg", "发生未知错误，请联系管理员！");
            return ERROR;
        }
    }

    public String showJudgeDetail() throws BaseException {
        Integer id = Integer.valueOf(judgeId);
        judge = service.updateInitJudge(id);
        judgeDetail = new JudgeExcelBean(judge);
        judgeDetail.decodeChangeLine();
        HttpSession session = ServletActionContext.getRequest().getSession();
        if (judgeDetail.getPhoto() != null && judgeDetail.getPhoto().length > 0) {
            session.setAttribute(BaseConstant.SESSION_USER_PHOTO, judgeDetail.getPhoto());
        } else {
            session.removeAttribute(BaseConstant.SESSION_USER_PHOTO);
        }
        return SUCCESS;
    }

    public String execute() {
        return SUCCESS;
    }

    public String exportInit() {
        return SUCCESS;
    }
}
